package nl.hanze.designpatterns.mvc.view;

import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SpringLayout;

import nl.hanze.designpatterns.domain.TroubleTicket;
import nl.hanze.designpatterns.mvc.model.MainModel;

public class AllTicketsView extends View{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private SpringLayout layout;
	private JLabel loginLabel;
	private JLabel username;
	private JScrollPane ticketPanel;
	private JList<TroubleTicket> allTickets;
	private JButton newTicket;
	private MainModel model;

	public AllTicketsView(MainModel model){
		this.init(model);
		this.addItems();
		this.locateItems();
	}
	
	private void init(MainModel model){
		this.model = model;
		this.layout = new SpringLayout();
		this.setLayout(layout);
		this.setBorder(BorderFactory.createTitledBorder("All Tickets"));
		
		this.loginLabel = new JLabel("Logged in as: ");
		this.username	= new JLabel(this.model.getLoginModel().getLoginCredential().getUserName());
		this.allTickets = new JList<TroubleTicket>();
		this.allTickets.setFixedCellHeight(55);
		this.allTickets.setFixedCellWidth(350);
		
		this.ticketPanel = new JScrollPane(allTickets);
		
		this.newTicket = new JButton("New Ticket");
		this.newTicket.setActionCommand("newTicket");
		this.newTicket.addActionListener(this);
	
	}
	
	private void addItems(){
		this.add(this.loginLabel);
		this.add(this.username);
		this.add(this.ticketPanel);
		this.add(this.newTicket);
	}
	
	private void locateItems(){
		//userLabel position
		this.layout.putConstraint(SpringLayout.WEST, this.loginLabel, 17, SpringLayout.WEST, this);
		this.layout.putConstraint(SpringLayout.NORTH, this.loginLabel, 10, SpringLayout.NORTH, this);
		//username position
		this.layout.putConstraint(SpringLayout.WEST, this.username, 100, SpringLayout.WEST, this);
		this.layout.putConstraint(SpringLayout.NORTH, this.username, 10, SpringLayout.NORTH, this);
		//userLabel position
		this.layout.putConstraint(SpringLayout.WEST, this.ticketPanel, 17, SpringLayout.WEST, this);
		this.layout.putConstraint(SpringLayout.NORTH, this.ticketPanel, 30, SpringLayout.NORTH, this);
		//newTicket position
		this.layout.putConstraint(SpringLayout.WEST, this.newTicket, 17, SpringLayout.WEST, this);
		this.layout.putConstraint(SpringLayout.NORTH, this.newTicket, 480, SpringLayout.NORTH, this);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e.getActionCommand());
	}

}
